/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
<template>
  <div class="user-manage">
    <h4 style="font-size: 16px;
      font-weight: 600;
      color: #333333;
      font-family: PingFang SC, PingFang SC-600;">关联权限用户列表</h4>

    <!-- 搜索框 -->
    <m-conditions
      :showSearch="true"
      @on-conditions="_onConditions"
    >
      <template slot="button-group">
        <el-button
          type="primary"
          size="mini"
          @click="changeRoleByIds"
        >更换角色</el-button>
      </template>
    </m-conditions>
    <!-- list -->
    <template v-if="roleList.length || total>0">
      <m-list
        ref="mList"
        @on-edit="_onEdit"
        @on-update="_onUpdate"
        :role-list="roleList"
        :page-no="searchParams.pageNo"
        :page-size="searchParams.pageSize"
        @changeRoleById="changeRoleById"
      >
      </m-list>
      <div class="page-box">
        <el-pagination
          background
          @current-change="_page"
          @size-change="_pageSize"
          :page-size="searchParams.pageSize"
          :current-page.sync="searchParams.pageNo"
          :page-sizes="[10, 30, 50]"
          layout="sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </template>
    <template v-if="!roleList.length && total<=0">
      <m-no-data></m-no-data>
    </template>
    <!-- change -->
    <el-dialog
      title="更换角色"
      v-if="changeRoleDialog"
      :visible.sync="changeRoleDialog"
      width="420"
      append-to-body
    >
      <m-popover
        ref="popover"
        @ok="_ok"
        @close="close"
      >
        <template slot="content">
          <div
            class="change-role-model"
            style="padding:40px"
          >
            <el-select
              v-model="selectRole"
              clearable
              placeholder="请选择"
              size="mini"
              style="width: 100%;"
            >
              <el-option
                v-for="item in roleLists"
                :key="item.id"
                :label="item.userGroup"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </div>
        </template>
      </m-popover>
    </el-dialog>
  </div>
</template>
<script>
  import _ from 'lodash'
  import { mapActions, mapGetters } from 'vuex'
  import mList from './list'
  import mNoData from '@/module/components/noData/noData'
  import listUrlParamHandle from '@/module/mixin/listUrlParamHandle'
  import mConditions from '@/module/components/conditions/conditions'
  import mPopover from '@/module/components/popup/popover'

  export default {
    name: 'userList',
    data () {
      return {
        total: null,
        isLoading: true,
        roleList: [],
        selectRole: '',
        selectList: [], // 多选数据
        searchParams: {
          pageSize: 10,
          pageNo: 1,
          searchVal: ''
        },
        isLeft: true,
        changeRoleDialog: false,
        item: {}
      }
    },
    mixins: [listUrlParamHandle],
    computed: {
      ...mapGetters('role', [
        'roleLists'
      ])
    },
    methods: {
      ...mapActions('permission', ['getRoleList']),
      /**
       * Query
       */
      _onConditions (o) {
        this.searchParams = _.assign(this.searchParams, o)
        this.searchParams.pageNo = 1
      },
      _page (val) {
        this.searchParams.pageNo = val
      },
      _pageSize (val) {
        this.searchParams.pageSize = val
      },
      _onUpdate () {
        this._debounceGET()
      },
      changeRoleByIds () {
        this.selectList = this.$refs.mList.selectList
        if (!(this.selectList && this.selectList.length > 0)) return this.$message.warning('请勾选关联用户')
        this.changeRoleDialog = true
      },
      changeRoleById (row) {
        this.selectList = [row]
        console.log('this.selectList', this.selectList)
        this.changeRoleDialog = true
      },
      _ok () {
        if (!this.selectRole) return this.$message.warning('请选择更换角色')
        this._debounceGET('false')
        this.changeRoleDialog = false
      },
      close () {
        this.changeRoleDialog = false
      },
      _getList (flag) {
        this.isLoading = !flag
        this.getRoleList(this.searchParams).then(res => {
          if (this.searchParams.pageNo > 1 && res.totalList.length === 0) {
            this.searchParams.pageNo = this.searchParams.pageNo - 1
          } else {
            this.roleList = []
            this.roleList = res.totalList
            this.total = res.total
            this.isLoading = false
          }
        }).catch(e => {
          this.isLoading = false
        })
      }
    },
    watch: {
      // router
      '$route' (a) {
        // url no params get instance list
        this.searchParams.pageNo = _.isEmpty(a.query) ? 1 : a.query.pageNo
      }
    },
    components: { mList, mConditions, mNoData, mPopover }
  }
</script>
<style lang="scss" scoped>
.user-manage {
  padding-right: 20px;
  /deep/.no-data-model {
    height: calc(100vh - 554px);
  }
}
</style>
